<template>
  <div class>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['act:busflow:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
        <el-button
          v-if="nodeType === 'new'"
          v-hasPermi="['act:busflow:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="copyHandle"
        >{{ $t("复制首轮投资节点") }}</el-button>
      </el-col>
    </el-row>
    <el-table
      v-loading="loading"
      tooltip-effect="darkTable"
      :data="wfConfigNodeList"
      border
      class="nowrapTable showOnlyLine"
      row-key="id"
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column :label="$t('节点名称')" align="left" prop="nodeName" width="200" fixed="left" />
      <el-table-column
        :label="$t('表单类型')"
        align="center"
        prop="formType"
        width="100"
        :formatter="formTypeFormat"
      />
      <el-table-column :label="$t('使用表单')" align="center" prop="formName" width="100" :formatter="formFormat" />
      <el-table-column
        :label="$t('对应流程')"
        align="center"
        prop="processKey"
        width="100"
        :formatter="processFormat"
      />
      <el-table-column :label="$t('依赖节点')" align="center" prop="relyNodeName" width="180">
        <template slot-scope="scope">
          <ToolTipBreak :content="$t(scope.row.relyNodeName)" />
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('审批完成后是否发起会议')"
        align="center"
        prop="meeting"
        width="180"
        :formatter="meetingFormat"
      />
      <el-table-column
        :label="$t('会议类型')"
        align="center"
        prop="meetingType"
        width="100"
        :formatter="meetingTypeFormat"
      />
      <el-table-column :label="$t('可以查看表单的角色')" align="center" prop="viewRoleName" width="180">
        <template slot-scope="scope">
          <ToolTipBreak :content="$t(scope.row.viewRoleName)" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('可以启动流程的角色')" align="center" prop="startRoleName" width="180">
        <template slot-scope="scope">
          <ToolTipBreak :content="$t(scope.row.startRoleName)" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('可以启动流程的部门')" align="center" prop="startDeptName" width="180">
        <template slot-scope="scope">
          <ToolTipBreak :content="$t(scope.row.startDeptName)" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('可以豁免流程的角色')" align="center" prop="immunityRoleName" width="180">
        <template slot-scope="scope">
          <ToolTipBreak :content="$t(scope.row.immunityRoleName)" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('什么状态下可以再次启动')" align="center" prop="restart" width="180" :formatter="restartFormat" />
      <el-table-column :label="$t('启动前校验的附件')" align="center" prop="checkFileName" width="180">
        <template slot-scope="scope">
          <ToolTipBreak :content="$t(scope.row.checkFileName)" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('审批完成后是否进入投后')" align="center" prop="after" width="180" :formatter="afterFormat" />
      <el-table-column
        :label="$t('操作')"
        align="center"
        fixed="right"
        width="140px"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['act:busflow:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['act:busflow:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加或修改业务流程节点对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="formOpen"
      width="70%"
      :close-on-click-modal="false"
      :lock-scroll="false"
      custom-class="dialogForm"
      :append-to-body="true"
    >
      <wfConfigNodeForm ref="wfConfigNodeForm" />
    </el-dialog>

    <el-dialog
      title="复制节点"
      :visible.sync="copyOpen"
      width="20%"
      :close-on-click-modal="false"
      :lock-scroll="false"
      append-to-body
      custom-class="dialogForm dialog-height"
      class="dialog-height"
    >
      <wf-config-node-select ref="wfConfigNodeSelect" />
    </el-dialog>
  </div>
</template>

<script>
import {
  listWfConfigNode,
  delWfConfigNode
} from '@/api/act/busFlow/wfConfigNode'
import wfConfigNodeForm from './wfConfigNodeForm.vue'
import wfConfigNodeSelect from './wfConfigNodeSelect'
import { listDataProcess } from '@/api/act/process/actProcess'
import { selectDictLabels } from '@/utils/ruoyi'
export default {
  components: {
    wfConfigNodeForm,
    wfConfigNodeSelect
  },
  props: {
    flowId: {
      type: String,
      require: true,
      default: ''
    },
    nodeType: {
      type: String,
      default: 'first'
    }
  },
  data() {
    return {
      // 流程配置的ID
      // flowId: undefined,
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 业务流程节点表格数据
      wfConfigNodeList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      formOpen: false,
      // 查询参数
      queryParams: {
        flowId: undefined,
        nodeType: undefined
      },
      // 表单类型选项
      formTypeOptions: [],
      // 是否字典选项
      yesNoOptions: [],
      // 会议类型
      mtTypeOptions: [],
      // 流程状态字典
      workFlowStatus: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 是否打开复制节点表单
      copyOpen: false
    }
  },
  watch: {
    flowId: {
      handler(val) {
        if (val) {
          this.init(val, this.nodeType)
        }
      },
      deep: true,
      immediate: true
    }
  },
  created() {},
  methods: {
    /** 查询业务流程节点列表 */
    getList(flowId, busType) {
      this.loading = true
      this.queryParams.flowId = flowId
      this.queryParams.nodeType = this.nodeType
      listWfConfigNode(this.queryParams).then(response => {
        this.wfConfigNodeList = response.rows
        this.busType = busType
        this.total = response.total
        this.loading = false
      })
    },

    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        parentId: undefined,
        nodeName: undefined,
        formType: undefined,
        formId: undefined,
        processKey: undefined,
        meeting: undefined,
        meetingType: undefined,
        viewRole: undefined,
        startRole: undefined,
        startDept: undefined,
        immunityRole: undefined,
        restart: undefined,
        checkFile: undefined,
        after: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
      this.formOpen = true
      this.title = this.$t('新增节点')
      this.$nextTick(() => {
        this.$refs.wfConfigNodeForm.init(null, this.flowId, this.nodeType)
      })
    },
    // 复制节点
    copyHandle() {
      this.copyOpen = true
      this.$nextTick(() => {
        this.$refs.wfConfigNodeSelect.init(this.flowId, this.nodeType)
      })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.title = this.$t(row.nodeName)
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.wfConfigNodeForm.init(row.id, this.flowId, row.nodeType)
      })
    },
    // 初始化数据
    async init(flowId, busType) {
      if (flowId !== null && flowId !== '') {
        this.flowId = flowId
        if (!(this.formTypeOptions && this.procDefKeyOptions && this.yesNoOptions && this.mtTypeOptions)) {
          await Promise.all(this.getDependencies())
        }
        this.getList(flowId, busType)
      }
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      })
        .then(function() {
          return delWfConfigNode(row.id)
        })
        .then(() => {
          this.getList(this.flowId)
          this.msgSuccess(this.$t('删除成功'))
        })
        .catch(function() {})
    },
    // 格式化表单类型
    formTypeFormat(row) {
      if (row.parentId !== '0') {
        return this.selectDictLabel(this.formTypeOptions, row.formType)
      }
      return ''
    },
    // 格式化表单
    formFormat(row) {
      if (row.parentId !== '0') {
        return row.formName
      }
      return ''
    },
    // 流程ID转流程名称
    processFormat(row) {
      var actions = []
      if (row.parentId !== '0') {
        Object.keys(this.procDefKeyOptions).map(key => {
          if (this.procDefKeyOptions[key].key === '' + row.processKey) {
            actions.push(this.procDefKeyOptions[key].name)
            return false
          }
        })
      }
      return actions.join('')
    },
    // 审批完成后是否进入投后
    afterFormat(row) {
      return this.selectDictLabel(this.yesNoOptions, row.after)
    },
    // 审批结束后是否发起会议
    meetingFormat(row) {
      return this.selectDictLabel(this.yesNoOptions, row.meeting)
    },
    meetingTypeFormat(row) {
      return this.selectDictLabel(this.mtTypeOptions, row.meetingType)
    },
    // 什么状态可以再次启动流程
    restartFormat(row) {
      return selectDictLabels(this.workFlowStatus, row.restart)
    },
    getDependencies() {
      return [
        // 表单类型
        this.getDicts('form_type').then(response => {
          this.formTypeOptions = response.data
        }),
        // 流程下拉框
        listDataProcess().then(response => {
          this.procDefKeyOptions = response.data
        }),
        // 是否字典
        this.getDicts('sys_yes_no').then(response => {
          this.yesNoOptions = response.data
        }),
        // 会议类型
        this.getDicts('mt_type').then(response => {
          this.mtTypeOptions = response.data
        }),
        // 流程状态字典
        this.getDicts('work_flow_status').then(response => {
          this.workFlowStatus = response.data
        })
      ]
    }
  }
}
</script>
<style lang="scss" scoped>
.el-table th {
  overflow: unset !important;
}
.tableDiv {
  position: relative;
  overflow: hidden;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  -webkit-box-flex: 1;
  -ms-flex: 1;
  flex: 1;
  width: 100%;
  max-width: 100%;
  font-size: 14px;
  color: #606266;
}
/deep/ .dialog-height.el-dialog{
  height: 75%;
}
</style>
